// -*- c++ -*-
//
// rss.cpp
//
// Copyright (C) 2005 Petter Stokke <ummo@hellokitty.com>
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the
// Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
//

#include "rss.h"

#include <kdebug.h>

// --- RSSFeed ---

RSSFeed::RSSFeed()
{}

bool RSSFeed::parse( QDomDocument& doc )
{
    QDomNode rdf = doc.namedItem( "RDF" );
    QDomNode channel = rdf.namedItem( "channel" );
    if ( !channel.isElement() )
        return false;
    QDomNode chTitle = channel.namedItem( "title" );
    if ( chTitle.isElement() )
        m_title = chTitle.toElement().text();
    QDomNode chLink = channel.namedItem( "link" );
    if ( chLink.isElement() )
        m_link = KURL( chLink.toElement().text() );
    QDomNode chDesc = channel.namedItem( "description" );
    if ( chDesc.isElement() )
        m_description = chDesc.toElement().text();

    QDomNodeList items = rdf.toElement().elementsByTagName( "item" );
    uint i;
    for ( i = 0; i < items.count(); i++ ) {
        RSSItem item;
        if ( !item.parse( items.item( i ).toElement() ) )
            return false;
        m_items.append( item );
        kdDebug() << "Parsed item \"" << item.title() << "\" " << item.link() << endl;
    }
    kdDebug() << "Parsed a total of " << count() << " items." << endl;
    return true;
}

const QString& RSSFeed::title() const
{
    return m_title;
}

const KURL& RSSFeed::link() const
{
    return m_link;
}

const QString& RSSFeed::description() const
{
    return m_description;
}

const QValueList<RSSItem>& RSSFeed::items() const
{
    return m_items;
}

uint RSSFeed::count() const
{
    return m_items.count();
}



// --- RSSItem ---

RSSItem::RSSItem()
{}

RSSItem::RSSItem( const RSSItem& o )
{
    m_title = o.m_title;
    m_description = o.m_description;
    m_link = o.m_link;
    m_creator = o.m_creator;
    m_subject = o.m_subject;
    m_date = o.m_date;
}

bool RSSItem::parse( QDomElement item )
{
    QDomNode _title = item.namedItem( "title" );
    if ( _title.isElement() )
        m_title = _title.toElement().text();
    else
        return false;
    QDomNode _desc = item.namedItem( "description" );
    if ( _desc.isElement() )
        m_description = _desc.toElement().text();
    QDomNode _link = item.namedItem( "link" );
    if ( _link.isElement() )
        m_link = KURL( _link.toElement().text() );
    else
        return false;
    QDomNode _creator = item.namedItem( "creator" );
    if ( _creator.isElement() )
        m_creator = _creator.toElement().text();
    QDomNode _date = item.namedItem( "date" );
    if ( _date.isElement() )
        m_date = QDateTime::fromString( _date.toElement().text(), Qt::ISODate );
    QDomNode _subject = item.namedItem( "subject" );
    if ( _subject.isElement() )
        m_subject = _subject.toElement().text();
    return true;
}

const QString& RSSItem::title() const
{
    return m_title;
}

const QString& RSSItem::description() const
{
    return m_description;
}

const KURL& RSSItem::link() const
{
    return m_link;
}

const QString& RSSItem::creator() const
{
    return m_creator;
}

const QString& RSSItem::subject() const
{
    return m_subject;
}

const QDateTime& RSSItem::date() const
{
    return m_date;
}

